import baseUrl from "@/utils/config.js"
export function request(options,closeLoading) { //封装的requset
	let {
		url,
		data,
		method
	} = options;
	console.log(options)
	uni.showLoading({ //请求数据时的loading
		title: '加载中...'
	})
	console.log(data)
	return new Promise((resolve, reject) => {
		let header = {
			// 'Content-type': 'application/json'
			'Content-type': 'application/x-www-form-urlencoded'
		}
		// 已经登录 带token
		if (uni.getStorageSync('token')) {
			header = {
				// 'token': '' + uni.getStorageSync('token'),
				// 'Content-type': 'application/json'
				"Auth-Token":  uni.getStorageSync('token'),
				'Content-type': 'application/x-www-form-urlencoded'
			}
		}
		uni.request({ //这里是uniapp网络请求的api
			url: baseUrl + url, //仅为示例，并非真实接口地址。
			data,
			method: method || "POST", //默认请求方式是get
			header: header,
			timeout: 180000,
			dataType: 'json',
			success: (res) => {
				if (res.statusCode == 200) {
					if (res.data.statu) {
						resolve(res.data)
					} else if (!res.statu) {
						uni.clearStorageSync();
						
						reject(res.data)
						showToast(res.data.msg)
					 }
				} else {
					showToast("服务异常,请联系管理员...")
				}
			},
			fail: (res) => {
				
				showToast("网络异常，请稍后再试...")
			},
			complete: () => {
				uni.hideLoading();
			}
		});
	})
}

export function showToast(title, time, icon) { //封装Toast
	uni.showToast({
		title: title || "加载中...",
		duration: time || 2000,
		icon: icon || "none"
	});
}

export function timeForMat(){
	// 获取当前日期时间
	const now = new Date();
	
	// 提取年、月、日、小时、分钟和秒
	const year = now.getFullYear();
	const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从0开始，所以需要加1
	const day = String(now.getDate()).padStart(2, '0');
	const hours = String(now.getHours()).padStart(2, '0');
	const minutes = String(now.getMinutes()).padStart(2, '0');
	const seconds = String(now.getSeconds()).padStart(2, '0');
	
	// 格式化为 "YYYY-MM-DD HH:mm:ss" 形式
	// const formattedDateTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
	const formattedDateTime = `${month}-${day} ${hours}:${minutes}`;
	return formattedDateTime
}